package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.PopularScienceViewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PopularScienceMapper;
import com.ruoyi.system.domain.PopularScience;
import com.ruoyi.system.service.IPopularScienceService;

/**
 * 科普知识Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-08
 */
@Service
public class PopularScienceServiceImpl implements IPopularScienceService
{
    @Autowired
    private PopularScienceMapper popularScienceMapper;

    @Autowired
    private PopularScienceViewMapper popularScienceViewMapper;

    @Override
    public List<PopularScience> getPopularScienceList(Map map) {
        String type = map.get("type") == null?"":map.get("type").toString();
        List<PopularScience> popularScienceList = popularScienceMapper.getPopularScienceList(type);
        return popularScienceList;
    }

    /**
     * 查询科普知识
     *
     * @param id 科普知识主键
     * @return 科普知识
     */
    @Override
    public PopularScience selectPopularScienceById(Long id)
    {
        return popularScienceMapper.selectPopularScienceById(id);
    }

    /**
     * 查询科普知识列表
     *
     * @param popularScience 科普知识
     * @return 科普知识
     */
    @Override
    public List<PopularScience> selectPopularScienceList(PopularScience popularScience)
    {
        List<PopularScience> popularScienceList = popularScienceMapper.selectPopularScienceList(popularScience);
        if(CollectionUtil.isNotEmpty(popularScienceList)){
            popularScienceList.stream().forEach(popularScience1 -> {
                    //根据 popularScience1的id获取查看人数
                    Long popularScienceId = popularScience1.getId();
                    Integer viewNum = popularScienceViewMapper.countUsers(popularScienceId);
                    popularScience1.setViewNum(viewNum);
            });
        }
        return popularScienceList;
    }

    /**
     * 新增科普知识
     *
     * @param popularScience 科普知识
     * @return 结果
     */
    @Override
    public int insertPopularScience(PopularScience popularScience)
    {
        popularScience.setCreateTime(DateUtils.getNowDate());
        if(StringUtils.isNotEmpty(popularScience.getPublishStatus()) && popularScience.getPublishStatus().equals("0")){
            popularScience.setPublishDate(new Date());
        }
        return popularScienceMapper.insertPopularScience(popularScience);
    }

    /**
     * 修改科普知识
     *
     * @param popularScience 科普知识
     * @return 结果
     */
    @Override
    public int updatePopularScience(PopularScience popularScience)
    {
        popularScience.setUpdateTime(DateUtils.getNowDate());
        if(StringUtils.isNotEmpty(popularScience.getPublishStatus()) && popularScience.getPublishStatus().equals("0")){
            popularScience.setPublishDate(new Date());
        }
        return popularScienceMapper.updatePopularScience(popularScience);
    }

    /**
     * 批量删除科普知识
     *
     * @param ids 需要删除的科普知识主键
     * @return 结果
     */
    @Override
    public int deletePopularScienceByIds(Long[] ids)
    {
        return popularScienceMapper.deletePopularScienceByIds(ids);
    }

    /**
     * 删除科普知识信息
     *
     * @param id 科普知识主键
     * @return 结果
     */
    @Override
    public int deletePopularScienceById(Long id)
    {
        return popularScienceMapper.deletePopularScienceById(id);
    }
}
